package com.examiner.controller;

import com.examiner.bean.CommonPerson;
import com.examiner.bean.Department;
import com.examiner.bean.test.NewTest;
import com.examiner.bean.test.NewTestUND;
import com.examiner.bean.vo.EditNewTestVo;
import com.examiner.bean.vo.NewTestVo;
import com.examiner.page.QueryInfo;
import com.examiner.result.Result;
import com.examiner.service.CommonPersonService;
import com.examiner.service.DepartmentService;
import com.examiner.service.NewTestService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("newTest")
public class NewTestController {
    @Resource
    private NewTestService newTestService;

    @Resource
    private CommonPersonService commonPersonService;

    @Resource
    private DepartmentService departmentService;

    /**
     * 获取部门和成员
     * @param superior
     * @return
     */
    @PostMapping("/getCommonPersonAndDepartmentList/{superior}")
    public Result<Object> getCommonPersonAndDepartmentList(@PathVariable("superior")String  superior){
        Map<String , Object> data = new HashMap<>();
        List<CommonPerson> commonPersonList = commonPersonService.getPersonList(superior);
        List<Department> departmentList = departmentService.getDepartmentList(superior);
        data.put("commonPersonList",commonPersonList);
        data.put("departmentList",departmentList);
        return Result.SUCCESS(data);
    }


    /**
     * 新增
     * @param newTestVo
     * @return
     */
    @PostMapping("/addNewTest")
    public Result<Object> addNewTest(@RequestBody NewTestVo newTestVo){
        try{
            newTestService.addNewTest(newTestVo);
        }catch (Exception e){
            e.printStackTrace();
            return Result.RESULT(201,"新建考评添加失败",false,null);
        }
        return Result.RESULT(200,"新建考评添加成功",true,null);
    }

    /**
     * 获取新建考评的分页列表
     * @param userName
     * @param query
     * @return
     */
    @PostMapping("/getNewTestList/{userName}")
    public Result<Object> getNewTestList(@PathVariable("userName")String userName,@RequestBody QueryInfo query){
        Map<String,Object> data = new HashMap<>();
        List<NewTest> newTestList;
        int total;
        try {
            newTestList = newTestService.getNewTestList(userName,query);
            total = newTestService.getNewTestListTotal(userName,query);
        }catch (Exception e){
            e.printStackTrace();
            return Result.RESULT(201,"获取新建考评列表失败",false,null);
        }
        data.put("newTestList",newTestList);
        data.put("total",total);
        return Result.RESULT(200,"获取新建考评列表成功",true,data);
    }

    /**
     * 根据id 获取新建考评的信息
     * @param id
     * @return
     */
    @PostMapping("/getNewTestById/{id}")
    public Result<Object> getNewTestById(@PathVariable("id")Integer id){
        Map<String,Object> data = new HashMap<>();
        NewTest newTest =  newTestService.getNewTestById(id);
        NewTestUND newTestVo = newTestService.getNewTestVoById(id);
        data.put("newTest",newTest);
        data.put("newTestVo",newTestVo);
        return Result.RESULT(200,"获取新建考评信息成功",true,data);
    }

    /**
     * 根据id 获取新建考评的信息
     * @param nid
     * @return
     */
    @PostMapping("/getNewTestByNid/{id}")
    public Result<Object> getNewTestByNId(@PathVariable("id")Integer nid){
        NewTest newTest =  newTestService.getNewTestById(nid);
        return Result.RESULT(200,"获取新建考评信息成功",true,newTest);
    }

    /**
     * 修改新建考评信息
     * @param editInfo
     * @return
     */
    @PostMapping("/changeNewTestInfo")
    public Result<Object> changeNewTestInfo(@RequestBody EditNewTestVo editInfo){
        try{
            newTestService.changeNewTestInfo(editInfo);
        }catch (Exception e){
            e.printStackTrace();
            return Result.RESULT(201,"修改新建考评信息失败",false,null);
        }
        return Result.RESULT(200,"修改新建考评信息成功",true,null);
    }

    @PostMapping("/deleteNewTestById/{id}")
    public Result<Object> deleteNewTestById(@PathVariable("id")Integer id){
        try{
            newTestService.deleteNewTestById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.RESULT(201,"删除新建考评失败",false,null);
        }
        return Result.RESULT(200,"删除新建考评成功",true,null);
    }



    /**
     * 改变当前考评的考评员
     */
    /*@PostMapping("/changeCommonPersonExaminer")
    public Result<Object> changeCommonPersonExaminer(@RequestBody CommonPerson commonPerson){
        if (commonPerson==null){ return Result.RESULT(201,"传入用户为空",false, null);}
        try{
            commonPersonService.changeExaminer(commonPerson);
        }catch (Exception e){
            e.printStackTrace();
            return Result.FAIL(null);
        }
        return Result.RESULT(200,"修改考评员成功",true,null);
    }*/

    /**
     * 改变当前考评的考评员
     */
    /*@PostMapping("/changeDepartmentExaminer")
    public Result<Object> changeDepartmentExaminer(@RequestBody Department department){
        if (department==null){ return Result.RESULT(201,"传入部门为空",false, null);}
        try{
            departmentService.changeExaminer(department);
        }catch (Exception e){
            e.printStackTrace();
            return Result.FAIL(null);
        }
        return Result.RESULT(200,"修改考评员成功",true,null);
    }*/

}
